package com.aabte;

import com.alibaba.fastjson.JSON;
import com.aabte.bean.Employee;
import com.aabte.bean.EmployeeExample;
import com.aabte.mapper.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Generator {

    @Test
    public void runMbgFromJavaWithXml() throws Exception {
        URL resource = Generator.class.getClassLoader().getResource("generatorConfig.xml");
        assert resource != null;
        String path = resource.getPath();

        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;

        File configFile = new File(path);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

    @Test
    public void testSelectAll() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            List<Employee> employees = mapper.selectByExample(null);
            employees.forEach(employee -> System.out.println(JSON.toJSONString(employee)));
        }
    }

    @Test
    public void testSelectById() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Employee employeeOne = mapper.selectByPrimaryKey(1);
            System.out.println(JSON.toJSONString(employeeOne));

            EmployeeExample employeeExample = new EmployeeExample();
            EmployeeExample.Criteria criteria1 = employeeExample.createCriteria();
            criteria1.andGenderEqualTo("1").andLastNameLike("%test%");

            EmployeeExample.Criteria criteria2 = employeeExample.createCriteria();
            criteria2.andLastNameLike("%m%");
            employeeExample.or(criteria2);

            List<Employee> employees = mapper.selectByExample(employeeExample);
            employees.forEach(employee -> System.out.println(JSON.toJSONString(employee)));
        }
    }

    private SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        return builder.build(inputStream);
    }

}
